import { createStore } from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import user from './modules/user';
import base from './modules/base';
import school from './modules/school';
import addUser from './modules/addUser';

const store = createStore({
  plugins: [
    createPersistedState({
      key: 'single_admin_vuex',
      storage: window.sessionStorage,
    }),
  ],
  namespaced: true,
  modules: {
    user,
    base,
    school,
    addUser,
  },
  actions: {
    // 清除所有模块的状态（用于退出登录）
    clearAllState({ commit }) {
      // 先删除 sessionStorage，避免 vuex-persistedstate 重新保存
      try {
        sessionStorage.removeItem('single_admin_vuex');
      } catch (error) {
        console.error('删除 sessionStorage 失败:', error);
      }
      // 清空所有模块的状态
      commit('user/CLEAR_USER_INFO');
      commit('school/RESET_SCHOOL_STATE');
      commit('addUser/CLEAR_ADD_USER');
      commit('base/CLEAR_BASE');
    },
  },
});
export default store;
